package com.zynote.nfhoujie.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.zynote.commons.base.BaseServiceHandler;
import com.zynote.commons.utils.SqlConditionObject;
import com.zynote.nfhoujie.dao.ICommodityDAO;
import com.zynote.nfhoujie.model.Commodity;
import com.zynote.nfhoujie.model.view.MultiCommodityView;
import com.zynote.nfhoujie.service.ICommodityService;

/**
 * 
 * @className:CommodityServiceImpl.java
 * @classDescription:商品服务层实现类
 * @author: AIDAN SU
 * @createTime: 2012-10-13
 *
 */

@Service
public class CommodityServiceImpl extends BaseServiceHandler implements ICommodityService {

	@Resource
	private ICommodityDAO commodityDAO ; 
	
	@Override
	public Commodity findCommodityById(Long id) {
		return commodityDAO.findCommodityById(id);
	}

	@Override
	public Long createCommodity(Commodity commodity) {
		return commodityDAO.createCommodity(commodity);
	}

	@Override
	public void updateCommodity(Commodity commodity) {
		commodityDAO.updateCommodity(commodity);
	}

	@Override
	public void deleteCommodity(Commodity commodity) {
		commodityDAO.deleteCommodity(commodity);
		
	}
	
	@Override
	public void deleteCommodityById(Long id) {
		commodityDAO.deleteCommodityById(id);
		
	}
	
	@Override
	public int countAllCommoditys() {
		return commodityDAO.countAllCommoditys();
	}
	
	@Override
	public int countTypeCommoditys(Integer parentCategory) {
		return commodityDAO.countTypeCommoditys(parentCategory);
	}
	
	@Override
	public int countCommoditys(Long uid) {
		return commodityDAO.countCommoditys( uid );
	}


	@Override
	public int countStatusCommoditys(Integer commodityStatus) {
		return commodityDAO.countStatusCommoditys(commodityStatus);
	}
	
	@Override
	public List<MultiCommodityView>listAllCommoditys(int startPosition,int pageSize ) {
		return commodityDAO.listAllCommoditys(startPosition, pageSize);
	}
	
	@Override
	public List<MultiCommodityView>listTypeCommoditys(int startPosition,int pageSize ,int parentCategory) {
		return commodityDAO.listTypeCommoditys(startPosition, pageSize, parentCategory);
	}
	
	@Override
	public List<MultiCommodityView>listMyCommoditys(int startPosition,int pageSize, Long uid) {
		return commodityDAO.listMyCommoditys(startPosition, pageSize, uid);
	}
	
	/**
	 * 商品列表按属性排序
	 * @return
	 */
	private Map<String,String> getCommodityOrderProperties(){
		Map<String,String> orderProperties = new HashMap<String,String>();
		orderProperties.put("id", "desc");		
		return orderProperties;
	}

	@Override
	protected List<SqlConditionObject> getSqlConditionList( Map<String, String[]> params) {
		
		List<SqlConditionObject> sqlConditionList  = new ArrayList<SqlConditionObject>();
		String[] commodityTitle = params.get("commodityTitle");
		String[] uid = params.get("uid");
		String[] childCategory = params.get("childCategory");
		String[] parentCategory = params.get("parentCategory");
		String[] writeTimeStart = params.get("writeTimeStart");
		String[] writeTimeEnd = params.get("writeTimeEnd");
		
		//标题
		addSqlCondition(sqlConditionList, "and", "commodity_title", "like", commodityTitle);
		//用户ID
		addSqlCondition(sqlConditionList, "and", "uid", "=", uid);
		//商品子类别
		addSqlCondition(sqlConditionList, "and", "child_category", "=", childCategory);
		//商品父类别
		addSqlCondition(sqlConditionList, "and", "parent_category", "=", parentCategory);
		//开始时间分类
		addSqlCondition(sqlConditionList, "and", "write_time", ">=", writeTimeStart);
		//结束时间分类
		addSqlCondition(sqlConditionList, "and", "write_time", "<=", writeTimeEnd);

		return sqlConditionList;
	}

}
